package tut.Struct.collection.phan1.map.weakHashMap.weakHashMapvsHashMap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

// xem thay doi thang someDataObject thi hash map co bi thay doi ko
public class HashMapDemo1 {

	public static void main(String[] args) {

		// add one element to the map...
		Map<String, Data> map = new HashMap<String, Data>();
		Data someDataObject = new Data("test");
		map.put(someDataObject.value, someDataObject);
		System.out.println("map contains someDataObject ? "
				+ map.containsKey(someDataObject));

		// now make someDataObject eligible for garbage collection...
		someDataObject = new Data( "new values");

		for (int i = 0; i < 1; i++) {
			if (map.size() != 0) {
				System.out
						.println("At iteration "
								+ i
								+ " the map still holds the reference on someDataObject");
			} else {
				System.out
						.println("someDataObject has finally been garbage collected at iteration "
								+ i + ", so the map is now empty");
				break;
			}

		}

		Set set = map.keySet();
		Iterator i2 = set.iterator();
		while (i2.hasNext()) {
			String key = (String) i2.next();
			Data value = (Data) map.get(key);
			System.out.println ("v= " +value.value);
		}
		System.out.println ("outside val= " + someDataObject.value);
	}
// ket luan thang trong hashmap ko bi thay doi ?

	static class Data {
		String value;

		Data(String value) {
			this.value = value;
		}
	}
}
